package com.ithuameng.admin.converter;

import com.google.common.collect.Maps;
import com.ithuameng.admin.enums.AdminEnumStrValue;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.Map;

/**
 * Convert To Enum Factory
 * 
 * @author ithuameng
 */
public final class String2EnumConverterFactory implements ConverterFactory<String, AdminEnumStrValue> {

	@SuppressWarnings("rawtypes")
	private static final Map<Class, Converter> CONVERTERS = Maps.newHashMap();

	@SuppressWarnings("unchecked")
	@Override
	public <T extends AdminEnumStrValue> Converter<String, T> getConverter(Class<T> targetType) {
		Converter<String, T> converter = CONVERTERS.get(targetType);
		if (converter == null) {
			converter = new String2EnumConverter<>(targetType);
			CONVERTERS.put(targetType, converter);
		}
		return converter;
	}
}
